package com.nowar.data.service.impl;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.nowar.common.exception.BBException;
import com.nowar.data.dao.UserInfoDao;
import com.nowar.data.entity.UserInfoEntity;
import com.nowar.data.entity.vo.DepartInfoVo;
import com.nowar.data.entity.vo.UserInfoVo;
import com.nowar.data.service.UserInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 谢无殇。
 * @Date : 2023-02-20 23:14
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public int insert(UserInfoEntity userInfo) {
        return 0;
    }

    @Override
    public UserInfoVo getUserInfoEntity(UserInfoEntity userInfo) {
        UserInfoEntity userInfoEntity = userInfoDao.getUserInfoEntity(userInfo);
        if (userInfoEntity == null) {
            return null;
        }
        return getUserInfoVo(userInfoEntity);
    }

    @Override
    public List<DepartInfoVo> getUserInfoList(UserInfoEntity userInfo) {
        List<UserInfoEntity> userInfoList = userInfoDao.getUserInfoList(userInfo.getUserCode(), userInfo);
        if (CollectionUtils.isEmpty(userInfoList)) {
            throw new BBException(101, "没有数据！");
        }
        List<DepartInfoVo> list = getDepartInfoList(userInfoList);
        Table<String, String, List<UserInfoVo>> table = HashBasedTable.create();
        userInfoList.forEach(userInfoVo -> {
            List<UserInfoVo> userInfoVos = table.get(userInfoVo.getManagementName(), userInfoVo.getDepartName());
            if (CollectionUtils.isEmpty(userInfoVos)) {
                userInfoVos = new ArrayList<>();
            }
            UserInfoVo user = UserInfoVo.builder()
                    .userCode(userInfoVo.getUserCode())
                    .userName(userInfoVo.getUserName())
                    .managementCode(userInfoVo.getManagementCode())
                    .managementName(userInfoVo.getManagementName())
                    .departCode(userInfoVo.getDepartCode())
                    .departName(userInfoVo.getDepartName())
                    .postCode(userInfoVo.getPostCode())
                    .postName(userInfoVo.getPostName())
                    .superLeader(userInfoVo.getSuperLeader())
                    .remarks(userInfoVo.getRemarks())
                    .createBy(userInfoVo.getCreateBy())
                    .createTime(userInfoVo.getCreateTime())
                    .build();
            userInfoVos.add(user);
            table.put(userInfoVo.getManagementName(), userInfoVo.getDepartName(), userInfoVos);
        });
        return list;
    }


    @Override
    public int updateUserInfo(UserInfoEntity userInfo) {
        return 0;
    }

    @Override
    public int deleteUserInfo(UserInfoEntity userInfo) {
        return 0;
    }

    private List<DepartInfoVo> getDepartInfoList(List<UserInfoEntity> userInfoList) {
        List<DepartInfoVo> list = new ArrayList<>();
        // 按管理中心进行分组
        Map<String, List<UserInfoEntity>> map = userInfoList.stream().collect(Collectors.groupingBy(UserInfoEntity::getManagementCode));
        map.forEach((managementCode, departInfo) -> {
            DepartInfoVo departInfoVo = new DepartInfoVo();
            departInfoVo.setDepartCode(managementCode);
            if (CollectionUtils.isNotEmpty(departInfo)) {
                // 按部门分组
                Map<String, List<UserInfoEntity>> departCodeMap = departInfo.stream().collect(Collectors.groupingBy(UserInfoEntity::getDepartCode));
                List<DepartInfoVo> departInfoList = new ArrayList<>();
                departCodeMap.forEach((departCode, userInfoVoList) -> {
                    DepartInfoVo userDepartInfo = new DepartInfoVo();
                    userDepartInfo.setDepartCode(departCode);
                    List<UserInfoVo> userInfos = new ArrayList<>();
                    if (CollectionUtils.isNotEmpty(userInfoVoList)) {
                        userInfoVoList.forEach((user) -> {
                            departInfoVo.setDepartName(user.getManagementName());
                            userDepartInfo.setDepartName(user.getDepartName());
                            userDepartInfo.setSuperDepartCode(user.getManagementName());
                            userInfos.add(getUserInfoVo(user));
                        });
                    }
                    userDepartInfo.setUserInfos(userInfos);
                });
                departInfoVo.setDepartInfos(departInfoList);
            }
            list.add(departInfoVo);
        });
        return list;
    }

    private UserInfoVo getUserInfoVo(UserInfoEntity userInfo) {
        return UserInfoVo.builder()
                .userCode(userInfo.getUserCode())
                .userName(userInfo.getUserName())
                .managementCode(userInfo.getManagementCode())
                .managementName(userInfo.getManagementName())
                .departCode(userInfo.getDepartCode())
                .departName(userInfo.getDepartName())
                .postCode(userInfo.getPostCode())
                .postName(userInfo.getPostName())
                .superLeader(userInfo.getSuperLeader())
                .remarks(userInfo.getRemarks())
                .createBy(userInfo.getCreateBy())
                .createTime(userInfo.getCreateTime())
                .build();
    }
}
